package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiU_match;
import cn.com.api.entity.ApiU_match_prize;
import cn.com.api.entity.ApiU_match_works;
import cn.com.api.mapper.ApiU_matchMapper;
import cn.com.api.mapper.ApiU_match_prizeMapper;
import cn.com.api.mapper.ApiU_match_worksMapper;
import cn.com.api.service.ApiU_matchService;
import cn.com.api.service.impl.base.U_matchServiceImplBase;
import cn.com.util.DateUtil;

/**
 * 
 **/
@Service
public class ApiU_matchServiceImpl extends U_matchServiceImplBase implements ApiU_matchService {
	@Autowired
	private ApiU_matchMapper apiU_matchMapper;

	@Autowired
	private ApiU_match_prizeMapper apiU_match_prizeMapper;
	@Autowired
	private ApiU_match_worksMapper apiU_match_worksMapper;

	private static Logger logger = Logger.getLogger(ApiU_matchServiceImpl.class);

	// 新增对象后返回记录ID
	public int U_matchInsertion(ApiU_match u_match) {
		return this.apiU_matchMapper.U_matchInsertion(u_match);
	}

	@Transactional
	@Override
	public int U_matchInfoInsertion(ApiU_match u_match, List<ApiU_match_prize> u_match_prize) {
		try {

			apiU_matchMapper.U_matchInsertion(u_match);
			int id = u_match.getMatch_id();
			for (ApiU_match_prize match_prize : u_match_prize) {
				match_prize.setMatch_id(id + "");
				apiU_match_prizeMapper.insertU_match_prize_info(match_prize);
			}
		} catch (Exception e) {
			logger.error("============[ApiU_matchServiceImpl[U_matchInfoInsertion[" + e + "]]==========");
		}
		return 0;

	}

	@Transactional
	@Override
	public int updateU_match_info(ApiU_match u_match, List<ApiU_match_prize> list_prize) throws Exception {

		apiU_match_prizeMapper.deleteU_match_prizeByMatchId(u_match.getMatch_id());
		for (ApiU_match_prize match_prize : list_prize) {
			apiU_match_prizeMapper.insertU_match_prize(match_prize);
		}
		return apiU_matchMapper.updateU_match_info(u_match);
	}

	@Override
	public List<ApiU_match> findU_matchs_condition(Map<String, Object> param) throws Exception {
		return apiU_matchMapper.findU_matchs_condition(param);
	}
	
	@Override
	public List<ApiU_match> findU_matchs_main(Map<String, Object> param) throws Exception {

		return apiU_matchMapper.findU_matchs_main(param);
	}
	
	
	@Override
	public List<ApiU_match> findU_matchs_award(Map<String, Object> param) throws Exception {

		return apiU_matchMapper.findU_matchs_award(param);
	}
	
	@Override
	public List<ApiU_match> findU_matchs_vote(Map<String, Object> param) throws Exception {

		return apiU_matchMapper.findU_matchs_vote(param);
	}
	
	

	@Override
	public List<ApiU_match> findU_matchs_enroll_condition(Map<String, Object> param) throws Exception {
		return apiU_matchMapper.findU_matchs_detail_condition(param);
	}

	@Override
	public List<ApiU_match> findU_matchs_vote_condition(Map<String, Object> param) throws Exception {

		return apiU_matchMapper.findU_matchs_detail_condition(param);
	}

	@Override
	public List<ApiU_match> findU_matchs_end_condition(Map<String, Object> param) throws Exception {

		return apiU_matchMapper.findU_matchs_detail_condition(param);
	}

	@Override
	public List<ApiU_match> findU_api_matchs_join(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_api_matchs_join(params);
	}

	@Override
	public List<ApiU_match> findU_api_matchs_nominate(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_api_matchs_nominate(params);
	}

	@Override
	public List<ApiU_match> findU_matchsByMatchName(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_matchsByMatchName(params);
	}

	@Override
	public List<ApiU_match> findU_api_matchs_vote(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_api_matchs_vote(params);
	}

	@Override
	public int findU_enroll_status(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_enroll_status(params);
	}

	@Override
	public int findU_vote_status(Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_vote_status(params);
	}

	@Override
	public Map<String, Object> findU_api_matchs_prizeInfo(String match_id) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		int vote_num = findU_api_matchs_votenum(match_id);
		int works_num = findU_api_matchs_worksnum(match_id);
		result.put("vote_num", vote_num);
		result.put("works_num", works_num);
		result.put("prize_num", 0);
		return result;
	}

	@Override
	public int findU_api_matchs_votenum(String match_id) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_api_matchs_votenum(match_id);
	}

	@Override
	public int findU_api_matchs_worksnum(String match_id) throws Exception {
		// TODO Auto-generated method stub
		return apiU_matchMapper.findU_api_matchs_worksnum(match_id);
	}

	// 统计投票结束的赛事
	@Transactional
	public void tongJiMatch() throws Exception {
		// 查找投票阶段已结束，未确定获奖名单的赛事
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("closetime", DateUtil.nowDateToString());
			map.put("is_award", 0);
			List<ApiU_match> matchs = apiU_matchMapper.findU_matchs_condition(map);
			// 要更新获奖字段的作品集合
			List<ApiU_match_works> updateworks = new ArrayList<ApiU_match_works>();
			for (ApiU_match match : matchs) {
				// 赛事的奖项
				List<ApiU_match_prize> match_prizes = match.getApiU_match_prizes();
				// 赛事的作品，已按投票的结果进行排序了
				List<ApiU_match_works> apiU_match_works = match.getApiU_match_works();
				Integer count = 0;// 人数额计数器
				for (ApiU_match_prize match_prize : match_prizes) {
					Integer endcount = count + Integer.valueOf(match_prize.getWinner_count() + "");
					for (int i = count; i < endcount; i++) {
						apiU_match_works.get(i).setPrize_id(match_prize.getPrize_id());
						updateworks.add(apiU_match_works.get(i));
					}
					count = endcount;
				}
			}
			// 批量更新作品的获奖信息
			apiU_match_worksMapper.updateMatchworksprize(updateworks);
			// 批量更新赛事是否确定获奖名单的信息
			apiU_matchMapper.updateMatchAward(matchs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Exception();
		}
	}

	@Override
	public List<ApiU_match> findU_api_matchs_win(Map<String, Object> params) throws Exception {

		return apiU_matchMapper.findU_api_matchs_mywin(params);
	}

	@Override
	public List<Map<String, Object>> findU_match_great(Map<String, Object> params) throws Exception {
		List<Map<String, Object>> listResult = new ArrayList<Map<String, Object>>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Map<String, Object>> list = apiU_match_worksMapper.findU_match_great(params);
		for (Map<String, Object> map : list) {
			resultMap = new HashMap<String, Object>();
			resultMap.put("works_id", map.get("WORKS_ID"));
			resultMap.put("join_name", map.get("JOIN_NAME"));
			resultMap.put("works_name", map.get("WORKS_NAME"));
			resultMap.put("works_url", map.get("WORKS_URL"));
			resultMap.put("match_id", map.get("MATCH_ID"));
			resultMap.put("user_id", map.get("USER_ID"));
			// resultMap.put("c_nick", map.get("C_NICK"));
			// resultMap.put("c_avatar", map.get("C_AVATAR"));
			// resultMap.put("c_tname", map.get("C_TNAME"));
			// resultMap.put("vote_num", map.get("VOTE_NUM"));
			// resultMap.put("prize_id", map.get("PRIZE_ID"));
			listResult.add(resultMap);
		}
		return listResult;

	}

}
